#pragma once
/**
 * @file LoopThreadPoll.hpp
 * @author 1102403684@qq.com
 * @brief 对LoopThread进行管理和分配
 * @version 0.1
 * @date 2024-10-17
 *
 * @copyright Copyright (c) 2024
 *
 */

#include <vector>
#include "LoopThread.hpp"

class LoopThreadPoll
{
public:
    LoopThreadPoll(EventLoop *baseloop):baseloop_(baseloop), thread_count_(0), next_loop_idx_(0){}

    void set_thread_count(int count){
        thread_count_ = count;
    }

    void create(){
        if(thread_count_ > 0){
            threads_.resize(thread_count_);
            loops_.resize(thread_count_);

            for(int i = 0;i<thread_count_;i++){
                threads_[i] = new LoopThread();
                loops_[i] = threads_[i]->get_loop();
            }
        }
    }

    EventLoop* next_loop(){
        if(thread_count_ == 0) return baseloop_;
        
        next_loop_idx_ = (next_loop_idx_ + 1) % thread_count_;
        return loops_[next_loop_idx_];
    }
private:
    int thread_count_;
    EventLoop *baseloop_; // 主reactor 运行在主线程

    std::vector<LoopThread *> threads_;
    std::vector<EventLoop* > loops_;
    int next_loop_idx_;
};